package javasec.seed.seeds;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.rmi.RemoteException;

import javasec.seed.DataSeedInterface;
import javasec.seed.Exposed;
import javasec.seed.Seed;
import javasec.seed.SeedServer;


public class NmapSeed extends Seed
{
	private static final long serialVersionUID = -6988469490919305936L;
	public DataSeedInterface data=SeedServer.dataSeed;
	
	public NmapSeed() throws RemoteException
	{
		super();

		data.createTable("HOST",new String[]
		{
			"HOST text unique not null",
			"A integer",
			"B integer",
			"C integer",

			"GATEWAY boolean",
			"NETWORK text"
		});
		
		// make sure that the SERVICE table exists
		data.createTable("SERVICE",new String[]
		{
			"HOST text not null",
			"PORT text not null",
			"PROTOCOL text",
			"APPLICATION text",
			"VERSION text"
		});
	}

	@Exposed(description="scan a remote host", arguments={"host"})
	public void scanHost(final String host)
	{
		// spwan off a new thread for the scanning
		new Thread()
		{
			public void run()
			{
				Process p=null;
				
				try
				{
					p=Runtime.getRuntime().exec("nmap -A -v "+host);
				}
				catch(IOException e)
				{
					System.out.println("unable to run scan");
					e.printStackTrace();
					return;
				}
				
				BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
				
				try
				{
					String line=null;
					while((line=reader.readLine())!=null)
					{
						String[] lineArray=line.split(" ");
						
						if(!lineArray[0].equals("Discovered"))continue;
						if(!(lineArray.length==6))continue;
						
						String[] servicePort=lineArray[3].split("/");
						String port=null;
						if(servicePort.length==2)
						{
							port=servicePort[0];
						}
						else System.out.println("strange nmap output..");
						
						// split the IP address
						String host=lineArray[5];
						String[] hostArray=host.split("\\.");
						if(hostArray.length!=4)
						{
							System.out.println("strange looking IP address");
							return;
						}
						
						// add the new host
						data.insertRow("HOST",new Object[]{host,hostArray[0],hostArray[1],hostArray[2],false,""});
				
						// make sure we haven't added this host/port pair
						Object[][] result=data.select("SERVICE",new Object[]{"HOST",host,"PORT",port});
						if(result==null || result.length>0)continue;
						
						// add the discovered service if it is new
						data.insertRow("SERVICE",new Object[]{host,port,portToProtocol(port),"",""});
					}
				}
				catch(Exception e)
				{
					System.out.println("error writing to console");
					e.printStackTrace();
					return;
				}
			}
		}.start();
	}
	
	private String portToProtocol(String port)
	{
		if(port.equals("21"))return "ftp";
		if(port.equals("22"))return "ssh";
		if(port.equals("23"))return "telnet";
		if(port.equals("80"))return "http";
		if(port.equals("443"))return "https";
		
		return "";
	}
}
